$(function () {
    $('#popup').click(function () {
        var html='<div id="alert"><div class="shade"></div>';
        html+='<div class="popup"><i></i>' ;
        html+='<a href="/qiantai"><dl><dt class="iconfont iconsy"></dt><dd>首页</dd></dl></a>';
        html+='<a href="/qiantai/quanzi.html"><dl><dt class="iconfont iconjiaoyi2"></dt><dd>社区</dd></dl></a>';
        html+='<a href="/qiantai/fenlei.html"><dl><dt class="iconfont iconfenlei"></dt><dd>分类</dd></dl></a>';
        html+='<a href="/qiantai/gouwuche.html"><dl><dt class="iconfont iconcart2"></dt><dd>购物车</dd></dl></a>';
        html+='<a href="/qiantai/my.html"><dl><dt class="iconfont iconuser"></dt><dd>我的</dd></dl></a>';
        html+='</div></div>';
        if (!$(this).hasClass('menu')){
            $(this).addClass('menu');
            $('body').css({'overflow':'hidden'}).append(html);
        }else {
            $(this).removeClass('menu');
            $('body').css({'overflow':'visible'});
            $('#alert').remove();
        }
        $('#alert .shade').click(function () {
            $('#popup').removeClass('menu');
            $('body').css({'overflow':'visible'});
            $('#alert').remove();
        });
    });
    $('.header a.back').click(function () {
        var url=$(this).data('url');
        if (url){
            location.href=url;
        } else {
            window.history.back(-1);
        }
    });
    $('.header .search input').focus(function () {
        $('body').css({'overflow':'hidden'});
        $('#popup').hide();
        $('#ajax_search').show();
        $('.search-info').animate({'top':'45px'},500);
        $('#search').show();
    });
    $('#search .shade').click(function () {
        var h=$('.search-info').outerHeight()+45;
        $('.header .search input').val('');
        $('body').css({'overflow':'visible'});
        $('.search-info').animate({'top':-h},500);
        $('#popup').show();
        $('#ajax_search').hide();
        setTimeout(function () {
            $('#search').hide();
        },500)
    });
    $('#search .search-list span').click(function () {
        var th=$(this).text();
        $('.header .search input').focus().val(th);
    });
    $(window).scroll(function () {
        trans();
    });
    trans();
    function trans() {
        var header=$('header');
        var s=$(document).scrollTop()*0.005;
        if (s>1){
            s=1;
        }
        if (header.hasClass('trs')){
            $('header').css('background',"rgba(255,255,255,"+s+")");
            // $('header').css('boxShadow',"0 3px 8px 0 rgba(221,82,77,"+s+")");
        }
    };
})
function yincang() {
    $('#search .shade').click();
}
function public_submit(url,data,obj1='') {
    if (obj1){
        var obj=obj1;
    }else {
        var obj=document.getElementById('submit');
    }
    var l = Ladda.create(obj);
    l.start();
    setTimeout(function () {
        l.stop();
        $(obj).attr('disabled',true);
        $.ajax({
            url:url,
            type:'post',
            data:data,
            dataType:'json',
            success:function (res) {
                if (res.code==1){
                    msg(res.msg,res.url);
                }else if (res.code==-100) {
                    msg(res.msg,res.url);
                }else {
                    msg(res.msg);
                }
            },error:function () {
                msg('网络错误,请稍后再试!')
            }
        });
        setTimeout(function () {
            $(obj).attr('disabled',false);
        },2000)
    },1000);
}

/**
 * 公共懒加载
 * @param elem 列表父级元素
 * @param url 提交的地址
 * @param data 要携带的参数类似&reid=1&type=2
 * @param empty 为空时显示的html
 */
function public_ajax(elem,url,data='',empty='暂无数据') {
    var empty='<div id="empty"><div class="empty iconfont iconzanwushuju"></div><h4>'+empty+'</h4></div>' ;
    layui.use('flow', function(){
        var flow = layui.flow;
        flow.load({
            elem: elem
            ,done: function(page, next){
                var lis = [];
                $.get(url+'?page='+page+data, function(res){
                    //假设你的列表返回在data集合中
                    layui.each(res.data, function(index, item){
                        lis.push(item);
                    });
                    if (res.data==''){
                        $('.layui-flow-more').hide();
                        $(elem).append(empty);
                    }
                    next(lis.join(''), page < res.pages);
                },'json');
            },end:'没有更多了'
        });
    });
}
function msg(msg,url='',time=2000) {
    layer.msg(msg,{time:time},function () {
        if (url){
            location.href=url;
        }
    });
}
var title=$('title').text();
function confirmmsg(msg,callback) {
    $(document).dialog({
        type : 'confirm',
        style: 'default',  // default、ios、android
        titleText: title,
        content: msg,
        onClickConfirmBtn:function () {
            callback();
        }
    });
}
function Verify(obj) {
    $(obj).attr('src','/index.php?m=Mobile&c=User&a=verify&r='+Math.random());
}
function check_mobile(mobile) {
    var filter=/^1[1-9]\d{9}$/;
    if (filter.test(mobile)){
        return true;
    }else {
        return false;
    }
}
/**
 * 发送验证码
 * @param obj
 * @param mobile1 手机号
 * @param type 类型 reg注册 forget忘记密码
 * @param time_out 超时时间
 */
function send_code(obj,mobile1,type='',time_out=60) {
    var mobile=mobile1;
    if (mobile==''){
        msg('请输入手机号码');
        $('[name=mobile]').focus();
        return false;
    }
    if (!check_mobile(mobile)){
        msg('手机号码格式有误');
        $('[name=mobile]').focus();
        return false;
    }
    var url="/qiantai/send_code";
    var data={mobile:mobile,type:type};
    $.ajax({
        url:url,
        type:'post',
        data:data,
        dataType:'json',
        success:function (res) {
            if (res.code==1){
                msg(res.msg);
                cutdown(obj,time_out);
            }else {
                msg(res.msg);
            }
        },error:function () {
            msg('网络错误,请稍后再试!')
        }
    });
}
function cutdown(obj,time_out){
    var text=$(obj).text();
    var time=time_out;
    var t=setInterval(function () {
        time--;
        if (time>0){
            $(obj).attr('disabled',true);
            $(obj).text(time+"s");
        }else {
            clearInterval(t);
            $(obj).attr('disabled',false);
            $(obj).text(text);
        }
    },1000);
}